<template>
  <div>
    <el-dialog :title="formData.id?'编辑页面':'添加页面'" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="formData" label-width="120px" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- 富文本 -->
          <quillEditor v-model="formData.content" @blur="$refs.form.validateField('content')" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="end">
          <el-button type="primary" size="small" @click="submit">确定</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 富文本
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { addPages, editPages } from '@/api/pages'
export default {
  components: {
    quillEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '', // 标题
        content: '' // 内容
      },
      rules: {
        title: [{ required: true, message: '该项不能为空哦', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 确定添加
    async submit() {
      // 校验
      await this.$refs.form.validate()

      if (this.formData.id) {
        // 编辑
        await editPages(this.formData)
      } else {
        // 新增
        await addPages(this.formData)
      }
      // 关闭弹窗
      this.closeFn()
      // 刷新列表
      this.$emit('update')
    },
    // 关闭弹窗
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        title: '', // 标题
        content: '' // 内容
      }
      // 清空校验规则
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
